//
//  GPTabBarViewController.m
//  GPFM
//
//  Created by 郭鹏 on 2018/2/7.
//  Copyright © 2018年 郭鹏. All rights reserved.
//

#import "GPTabBarViewController.h"
#import "GPNavigationViewController.h"
#import "UIImage+XMGImage.h"
#import "GPMiddleView.h"
#import "GPTabBar.h"

@interface GPTabBarViewController ()

@end

@implementation GPTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpTabbar];
}

+ (instancetype)shareInstance
{
    static GPTabBarViewController *tabbarC;
    
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        tabbarC = [[GPTabBarViewController alloc]init];
    });
    return tabbarC;
}
- (void)setUpTabbar {
    [self setValue:[[GPTabBar alloc] init] forKey:@"tabBar"];
}
//+ (instancetype)tabBarControllerWithAddChildVCsBlock: (void(^)( GPTabBarViewController *tabBarC))addVCBlock
//{
//    GPTabBarViewController *tabbarVC = [[GPTabBarViewController alloc] init];
//
//    if (addVCBlock) {
//        addVCBlock(tabbarVC);
//    }
//
//    return tabbarVC;
//}
+ (instancetype)tabBarControllerWithAddChildVCsBlock:(addVcBlock)vcBlock
{
    GPTabBarViewController *tabbarVC = [[GPTabBarViewController alloc] init];

    if (vcBlock) {
        vcBlock(tabbarVC);
    }
    return tabbarVC;
}
- (void)addChildVC: (UIViewController *)vc normalImageName: (NSString *)normalImageName selectedImageName:(NSString *)selectedImageName isRequiredNavController: (BOOL)isRequired {
    
    if (isRequired) {
        GPNavigationViewController *nav = [[GPNavigationViewController alloc] initWithRootViewController:vc];
        nav.tabBarItem = [[UITabBarItem alloc] initWithTitle:nil image:[UIImage originImageWithName:normalImageName] selectedImage:[UIImage originImageWithName:selectedImageName]];
        
        [self addChildViewController:nav];
    }else {
        [self addChildViewController:vc];
    }
}

- (void)setSelectedIndex:(NSUInteger)selectedIndex
{
    [super setSelectedIndex:selectedIndex];
    
    UIViewController *vc = self.childViewControllers[selectedIndex];
    if (vc.view.tag == 666) {
        vc.view.tag = 888;
        
        GPMiddleView *middleView = [GPMiddleView middleView];
        middleView.middleClickBlock = [GPMiddleView shareInstance].middleClickBlock;
        middleView.isPlaying = [GPMiddleView shareInstance].isPlaying;
        middleView.midImage = [GPMiddleView shareInstance].midImage;
        CGRect frame = middleView.frame;
        frame.size.width = 65;
        frame.size.height = 65;
        CGSize screenSize = [UIScreen mainScreen].bounds.size;
        frame.origin.x = (screenSize.width - 65) * 0.5;
        frame.origin.y = screenSize.height - 65;
        middleView.frame = frame;
        [vc.view addSubview:middleView];
        
    }
    
}

@end
